<template>
	<view class="container">
		<!-- 导航栏 -->
		<uni-nav-bar dark :fixed="true" shadow background-color="#007AFF" status-bar left-icon="left" left-text="返回"
			titleStyle="{height: '52px'}" :title="sample.name" @clickLeft="back">
			<block slot="right">
				<view class="city">
					<text  style="font-size: 15px;" @click="submit">确认归库</text>
				</view>
			</block>
		</uni-nav-bar>
		<uni-section title="样品归还信息" type="line">
			<view class="example">
				<!-- 基础用法，不包含校验规则 -->
				<uni-forms ref="baseForm"  labelPosition="top" :rules="rules">
					<uni-forms-item label="领样人">
						<uni-easyinput v-model="sample.consumerName" disabled=""/>
					</uni-forms-item>
					<uni-forms-item label="归还人">
						<uni-easyinput v-model="sample.returneeName" disabled=""/>
					</uni-forms-item>
					<uni-forms-item label="归还时间" name="returnTime" required="">
						<uni-datetime-picker type="datetime" v-model="sample.returnTime" />
					</uni-forms-item>
					<uni-forms-item label="归还位置" name="returnLocation" required="">
						<uni-data-checkbox v-model="sample.returnLocation"  :localdata="locations" mode="button" @change="changeReturnLocation"/>
						<uni-easyinput v-if="this.showCustomReturnLocation" v-model="sample.customReturnLocation"
							placeholder="请输入其他地点信息" />
					</uni-forms-item>
					<uni-forms-item label="归还照片" name="returnImg">
						<view class="example">
							<uni-file-picker v-model="sample.returnImgLists" limit="3" title="最多选择3张图片" ref="files" :auto-upload="false" file-mediatype="image">
							</uni-file-picker>
						</view>
					</uni-forms-item>
					<uni-forms-item label="其他说明" required="">
						<uni-easyinput type="textarea" v-model="sample.returnComment" placeholder="请输入其他说明"></uni-easyinput>
					</uni-forms-item>
				</uni-forms>
			</view>
		</uni-section>
	</view>
</template>

<script>
	export default {
		data() {
			return {
				sample: {
					returnImgLists:[]
				},
				locations: [],
				showCustomReturnLocation:false,
				selectedOrgName: '',
				selectedUserName: '',
				orgNames: [],
				orgs: [],
				userNames: [],
				users: [],
				loading: false,
				rules: {
					// 对department字段进行必填验证
					returnTime: {
						rules: [{
							required: true,
							errorMessage: '请选择归还时间',
						}]
					},
					returnLocation: {
						rules: [{
							required: true,
							errorMessage: '请选择归还位置',
						}]
					},
					returnLocation: {
						rules: [{
							required: true,
							errorMessage: '请选择归还位置',
						}]
					},
				}
			}
		},
		onLoad(option) {
			this.loadSampleInfo(option.id);
			// 加载样品存放地点(字典数据，Location)
			this.loadDictData('Location');
		},
		computed: {},
		methods: {
			//是否存放地址为自定义
			changeReturnLocation(location){
				if(location.detail.value=='custom'){
					this.showCustomReturnLocation = true;
				}
			},
			loadSampleInfo(sampleId){
				var this_ = this;
				this.$u.api.querySampleInfo(sampleId).then(res=>{
					this_.sample = res.data;
					this_.sample.returnee = this_.$store.state.vuex_user.userId;
					this_.sample.returneeName = this_.$store.state.vuex_user.nickName;
					//拆分归库照片数据,供组件使用
					if(this_.sample.returnImgs){
						this_.sample.returnImgLists = this_.sample.returnImgs.split(',');
						//去掉最后一个空对象
						this_.sample.returnImgLists.pop();
					}
						
				});
			},
			loadDictData(dictType) {
				var this_ = this; //存储this
				this_.$u.api.getDictData({
					"dictType": 'Location'
				}).then((response) => {
					let locations = [response.data.length];
					for (let index = 0; index < response.data.length; index++) {
						locations[index] = {
							'text': response.data[index].dictLabel,
							'value': response.data[index].dictValue
						};
					}
					locations[locations.length] = {
						'text': '其他地点',
						'value': 'custom'
					};
					this_.sample.location = locations[0].value;
					this_.locations = locations;
				});
			},
			back() {
				this.backPageRefresh();
			},
			submit() {
				var this_ = this;
				
				if(this_.$refs.files.files && this_.$refs.files.files.length < 1){
					this_.$u.toast('请选择归还样品照片');
					return;
				}
				this.$refs.baseForm.validate().then(res => {
					//先上传图片,再提交
					let temp_imgs = [];
					this_.$refs.files.files.forEach((item)=>{
						temp_imgs.push({
							name:item.name,
							uri:item.url
						})
					})
					this.$u.api.uploadFiles({
						files: temp_imgs,
						name: 'file'
					}).then(response => {
						if(response.data){
							var returnImgs = '';
							response.data.forEach((i,e)=>{
								returnImgs += (i.fileName+",")
							})
							this_.sample.returnImgs = returnImgs;
							this.$u.api.forwardSample(this_.sample).then(response => {
								this_.$u.toast('归还申请成功');
								this_.back();
							});
						}else{
							this_.$u.toast('归还申请失败');
						}

					});
				}).catch(err => {
					console.log('表单错误信息：', err);
					this.$u.toast('必填信息不能为空');
				})
			}
		}
	}
</script>

<style>
	.uni-section {
		margin-top: 0px !important;
		background-color: #eee;
	}

	.custom-image-box {
		/* #ifndef APP-NVUE */
		display: flex;
		/* #endif */
		flex-direction: row;
		justify-content: space-between;
		align-items: center;
	}

	.example {
		background-color: white;
		padding: 10px 20px 15px 20px;
	}

	.segmented-control {
		margin-bottom: 10px;
	}

	.button-group {
		margin-top: 15px;
		display: flex;
		justify-content: space-around;
	}

	.form-item {
		display: flex;
		align-items: center;
	}

	.button {
		display: flex;
		align-items: center;
		height: 35px;
		margin-left: 10px;
	}
</style>
